import UIKit

struct Platform {
    static let isSimulator: Bool = {
        var isSim = false
#if targetEnvironment(simulator)
        isSim = true
#else
        isSim = false
#endif
        return isSim
    }()
}

extension UIViewController {
    
    var topVC: UIViewController? {
        var resultVC: UIViewController?
        resultVC = _topVC(UIApplication.shared.keyWindow?.rootViewController)
        while resultVC?.presentedViewController != nil {
            resultVC = _topVC(resultVC?.presentedViewController)
        }
        return resultVC
    }
    
    private  func _topVC(_ vc: UIViewController?) -> UIViewController? {
        if vc is UINavigationController {
            return _topVC((vc as? UINavigationController)?.topViewController)
        } else if vc is UITabBarController {
            return _topVC((vc as? UITabBarController)?.selectedViewController)
        } else {
            return vc
        }
    }
    
    /// - Returns: 当前屏幕显示的VC
    class func getCurrentViewController() -> UIViewController?{
        // 获取当先显示的window
        var currentWindow = UIApplication.shared.keyWindow ?? UIWindow()
        if currentWindow.windowLevel != UIWindow.Level.normal {
            let windowArr = UIApplication.shared.windows
            for window in windowArr {
                if window.windowLevel == UIWindow.Level.normal {
                    currentWindow = window
                    break
                }
            }
        }
        return UIViewController.getNextXController(nextController: currentWindow.rootViewController)
    }
    
    private class func  getNextXController(nextController: UIViewController?) -> UIViewController? {
        if nextController == nil {
            return nil
        }else if nextController?.presentedViewController != nil {
            return UIViewController.getNextXController(nextController: nextController?.presentedViewController)
        }else if let tabbar = nextController as? UITabBarController {
            return UIViewController.getNextXController(nextController: tabbar.selectedViewController)
        }else if let nav = nextController as? UINavigationController {
            return UIViewController.getNextXController(nextController: nav.getVisibleViewController)
        }
        return nextController
    }
    
}

extension UIViewController {
    
    func showMessage(message: String) {
        showMessage(message: message.localized, afterDelay: CGFloat.app_hubInterval)
    }
    func showMessage(message: String, afterDelay: TimeInterval = CGFloat.app_hubInterval) {
        CommonToast.shared.showToast(message.localized, duration: afterDelay)
    }
    
    func showIndicator() {
        CommonToast.shared.showIndicator()
    }
    
    func hideIndicator() {
        CommonToast.shared.hideIndicator()
    }
    
}

extension UIViewController {
    
    /// The visible view controller from a given view controller
    var visibleViewController: UIViewController? {
        if let navigationController = self as? UINavigationController {
            return navigationController.topViewController?.visibleViewController
        } else if let tabBarController = self as? UITabBarController {
            return tabBarController.selectedViewController?.visibleViewController
        } else if let presentedViewController = presentedViewController {
            return presentedViewController.visibleViewController
        } else {
            return self
        }
    }
    
    /// The visible view controller from a given view controller
    var getVisibleViewController: UIViewController? {
        
        if let navigationController = self as? UINavigationController {
            return navigationController.topViewController?.visibleViewController
        } else if let tabBarController = self as? UITabBarController {
            return tabBarController.selectedViewController?.visibleViewController
        } else if let presentedViewController = presentedViewController {
            return presentedViewController.visibleViewController
        } else {
            return self
        }
        
    }
    
}

// MARK: - 获取当前widows所有的controler
extension UIViewController {
    
    class func getAllViewControllers() -> [UIViewController] {
        var allViewControllers = [UIViewController]()
        
        if let appDelegate = UIApplication.shared.delegate as? AppDelegate,
           let window = appDelegate.window {
            if let rootViewController = window.rootViewController {
                allViewControllers = getAllViewControllers(from: rootViewController)
            }
        }
        
        return allViewControllers
    }
    
    private class func getAllViewControllers(from viewController: UIViewController) -> [UIViewController] {
        var allViewControllers = [UIViewController]()
        
        allViewControllers.append(viewController)
        
        if let navigationController = viewController as? UINavigationController {
            allViewControllers += navigationController.viewControllers.flatMap { getAllViewControllers(from: $0) }
        } else if let tabBarController = viewController as? UITabBarController {
            if let selectedViewController = tabBarController.selectedViewController {
                allViewControllers += getAllViewControllers(from: selectedViewController)
            }
        }
        
//        allViewControllers += viewController.children.flatMap { getAllViewControllers(from: $0) }
        
        return allViewControllers
    }
    
}



